package com.xiansi.service;

import com.xiansi.mapper.AdminroleMapper;
import com.xiansi.mapper.AdminsMapper;
import com.xiansi.pojo.Adminrole;
import com.xiansi.pojo.AdminroleExample;
import com.xiansi.pojo.Admins;
import com.xiansi.pojo.AdminsExample;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service

public class AdminsService {
    @Resource
    private AdminsMapper adminsMapper;
    @Resource
    private AdminroleMapper adminroleMapper;

    /**
     * 登录
     *
     * @param loginName
     * @param adminPwd
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public List<Admins> login(String loginName, String adminPwd) {
        //多条件
        AdminsExample adminsExample = new AdminsExample();
        //创建容器
        AdminsExample.Criteria criteria = adminsExample.createCriteria();

        boolean name = loginName != null && !"".equals(loginName.trim());
        boolean password = adminPwd != null && !"".equals(adminPwd.trim());
        if (name && password) {
            criteria.andLoginNameEqualTo(loginName);
            criteria.andAdminPwdEqualTo(adminPwd);
            List<Admins> admins = adminsMapper.selectByExample(adminsExample);
            for (Admins admin : admins) {
                admin.setLastLoginTime(new Date());
                adminsMapper.updateByPrimaryKeySelective(admin);
            }
            return admins;
        }
        return null;
    }

    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public Adminrole queryAdminroleById(Integer id) {

        return adminroleMapper.selectByPrimaryKey(id);

    }


}
